import order from '@/api/order'
import common from './common'

const state = {
	orderNum: [],
	orders:{
		data:[],
		ajaxing: false,
		allLoaded: false
	},
	orderCancle:{
		ajaxing: false
	},
	orderConfirm: {
		ajaxing: false
	},
	orderDetail: {
		goods_info: [],
		address_info: {}
	},
	orderStatus:{
		1: {
			title: '待付款',
			actions: ['cancle', 'pay']
		},
		2:{
			title: '待发货',
			actions: ['refund'],
			goodsActions: ['refund']
		},
		3: {
			title: '待收货',
			actions: ['delivery', 'confirm'],
			goodsActions: ['refund'],
		},
		4: {
			title: '待评价',
			actions: ['comment'],
			goodsActions: ['refund']
		},
		5: {
			title: '退款'
		},
		6: {
			title: '退货'
		},
		7: {
			title: '交易关闭'
		},
		8: {
			title: '交易成功'
		},
		9: {
			title: '退款处理中'
		},
		10: {
			title: '退货处理中'
		},
		11: {
			title: '退货完成'
		},
		12: {
			title: '换货完成'
		},
		13: {
			title: '退款申请中'
		},
		14: {
			title: '退款完成'
		}
	},
	orderActions: {
		pay: {
			title: '付款',
			highlight: true
		},
		cancle: {
			title: '取消订单'
		},
		delivery: {
			title: '查看物流'
		},
		confirm: {
			title: '确认收货',
			highlight: true
		},
		comment: {
			title: '评价',
			highlight: true
		},
		refund: {
			title: '申请退款'
		}
	},
	// 难以理解…居然商品状态1~12只有订单在9~14中才会出现…否则要根据订单状态来
	goodsStatus: {
		0: {
			title: ''
		},
		1:{
			title: '换货申请中',
			actions: []
		},
		2:{
			title: '买家待发货',
			actions: []
		},
		3: {
			title: '卖家待收货',
			actions: []
		},
		4: {
			title: '卖家待发货',
			actions: []
		},
		5: {
			title: '',
			actions: []
		},
		6: {
			title: ''
		},
		7: {
			title: ''
		},
		8: {
			title: ''
		},
		9: {
			title: ''
		},
		10: {
			title: ''
		},
		11: {
			title: '退款申请中'
		},
		12: {
			title: ''
		},
		13: {
			title: ''
		},
		14: {
			title: ''
		}
	},
	goodsActions: {
		refund: {
			title: '申请退换货'
		},
		comment: {
			title: '评价',
			highlight: true
		}
	}
}

const getters = {
	orderNum: state => state.orderNum,
	list: state => state.orders.data,
	detail: state => state.orderDetail,
	orderStatus: state => state.orderStatus,
	orderActions: state => state.orderActions,
	goodsStatus: state => state.goodsStatus,
	goodsActions: state => state.goodsActions,
	ajaxing: state => state.orders.ajaxing,
	allLoaded: state => state.orders.allLoaded,
}

const actions = {
	async getOrderNum({commit}){
		const r = await order.getOrderNum()
		commit('orderNum', r.data)
	},
	async getOrderList({commit}, {type, page}) {
		commit('ajaxing', {name: 'orders', ajaxing: true})
		const {data, success} = await order.getOrderList({type, page})
		commit('ajaxing', {name: 'orders', ajaxing: false})
		commit('orderList', data.orders)
		if(!data.orders.length) commit('allLoaded')
	},
	async toCancleOrder({commit}, orderId) {
		commit('ajaxing', {name: 'orderCancle', ajaxing: true})
		const {success} = await order.cancleOrder(orderId)
		commit('ajaxing', {name: 'orderCancle', ajaxing: false})
		commit('cancleOrder', orderId)
	},
	async toConfirmOrder({commit}, orderId) {
		commit('ajaxing', {name: 'orderConfirm', ajaxing: true})
		const {success} = await order.confirmOrder(orderId)
		commit('ajaxing', {name: 'orderConfirm', ajaxing: false})
		commit('confirmOrder', orderId)
	},
	async getOrderDetail({commit}, orderId) {
		const {success, data} = await order.getOrderDetail(orderId)
		commit('orderDetail', data)
	},
	// 根据orderFormLogId获取订单号
	async getOrderNo({commit}, orderFormLogId) {
		const {success, data} = await order.getOrderNo(orderFormLogId)
		return data
	},
	async writeComment({commit}, {orderId, goodsId, content}) {
		const {success, data, msg} = await order.writeComment({orderId, goodsId, content})
		return {success, msg}
	},
	async refund({commit}, {orderId, goodsId, type, reason, num}) {
		const {success, data, msg} = await order.refund({orderId, goodsId, type, reason, num})
		return {success, msg}
	}
}

const mutations = {
	orderNum(state, nums) {
		state.orderNum = nums
	},
	orderList(state, data) {
		state.orders.data.push(...data)
	},
	reset(state) {
		state.orders.data = []
		state.orders.allLoaded = false
	},
	allLoaded(state) {
		state.orders.allLoaded = true
	},
	requestSend: common.requestSend,
	ajaxing: common.ajaxing,
	cancleOrder(state, orderId) {
		const order = state.orders.data.find(o => o.order_info.order_id == orderId)
		order.order_info.status = 7
	},
	confirmOrder(state, orderId) {
		const order = state.orders.data.find(o => o.order_info.order_id == orderId)
		order.order_info.status = 4
	},
	orderDetail(state, data) {
		state.orderDetail = data
	}
}

export default {
  state,
  getters,
  actions,
  mutations,
  namespaced: true
}